{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\nWriting mathematical expressions\n================================\n\nAn introduction to writing mathematical expressions in Matplotlib.\n\nYou can use a subset TeX markup in any matplotlib text string by placing it\ninside a pair of dollar signs ($).\n\nNote that you do not need to have TeX installed, since Matplotlib ships\nits own TeX expression parser, layout engine, and fonts.  The layout engine\nis a fairly direct adaptation of the layout algorithms in Donald Knuth's\nTeX, so the quality is quite good (matplotlib also provides a ``usetex``\noption for those who do want to call out to TeX to generate their text (see\n:doc:`/tutorials/text/usetex`).\n\nAny text element can use math text.  You should use raw strings (precede the\nquotes with an ``'r'``), and surround the math text with dollar signs ($), as\nin TeX. Regular text and mathtext can be interleaved within the same string.\nMathtext can use DejaVu Sans (default), DejaVu Serif, the Computer Modern fonts\n(from (La)TeX), `STIX <http://www.stixfonts.org/>`_ fonts (with are designed\nto blend well with Times), or a Unicode font that you provide.  The mathtext\nfont can be selected with the customization variable ``mathtext.fontset`` (see\n:doc:`/tutorials/introductory/customizing`)\n\nHere is a simple example::\n\n    # plain text\n    plt.title('alpha > beta')\n\nproduces \"alpha > beta\".\n\nWhereas this::\n\n    # math text\n    plt.title(r'$\\alpha > \\beta$')\n\nproduces \":mathmpl:`\\alpha > \\beta`\".\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>Mathtext should be placed between a pair of dollar signs ($). To make it\n   easy to display monetary values, e.g., \"$100.00\", if a single dollar sign\n   is present in the entire string, it will be displayed verbatim as a dollar\n   sign.  This is a small change from regular TeX, where the dollar sign in\n   non-math text would have to be escaped ('\\\\\\$').</p></div>\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>While the syntax inside the pair of dollar signs ($) aims to be TeX-like,\n   the text outside does not.  In particular, characters such as::\n\n     # $ % & ~ _ ^ \\ { } \\( \\) \\[ \\]\n\n   have special meaning outside of math mode in TeX.  Therefore, these\n   characters will behave differently depending on the rcParam ``text.usetex``\n   flag.  See the :doc:`usetex tutorial </tutorials/text/usetex>` for more\n   information.</p></div>\n\nSubscripts and superscripts\n---------------------------\n\nTo make subscripts and superscripts, use the ``'_'`` and ``'^'`` symbols::\n\n    r'$\\alpha_i > \\beta_i$'\n\n\\begin{align}\\alpha_i > \\beta_i\\end{align}\n\nSome symbols automatically put their sub/superscripts under and over the\noperator.  For example, to write the sum of :mathmpl:`x_i` from :mathmpl:`0` to\n:mathmpl:`\\infty`, you could do::\n\n    r'$\\sum_{i=0}^\\infty x_i$'\n\n\\begin{align}\\sum_{i=0}^\\infty x_i\\end{align}\n\nFractions, binomials, and stacked numbers\n-----------------------------------------\n\nFractions, binomials, and stacked numbers can be created with the\n``\\frac{}{}``, ``\\binom{}{}`` and ``\\genfrac{}{}{}{}{}{}`` commands,\nrespectively::\n\n    r'$\\frac{3}{4} \\binom{3}{4} \\genfrac{}{}{0}{}{3}{4}$'\n\nproduces\n\n\\begin{align}\\frac{3}{4} \\binom{3}{4} \\stackrel{}{}{0}{}{3}{4}\\end{align}\n\nFractions can be arbitrarily nested::\n\n    r'$\\frac{5 - \\frac{1}{x}}{4}$'\n\nproduces\n\n\\begin{align}\\frac{5 - \\frac{1}{x}}{4}\\end{align}\n\nNote that special care needs to be taken to place parentheses and brackets\naround fractions.  Doing things the obvious way produces brackets that are too\nsmall::\n\n    r'$(\\frac{5 - \\frac{1}{x}}{4})$'\n\n.. math ::\n\n    (\\frac{5 - \\frac{1}{x}}{4})\n\nThe solution is to precede the bracket with ``\\left`` and ``\\right`` to inform\nthe parser that those brackets encompass the entire object.::\n\n    r'$\\left(\\frac{5 - \\frac{1}{x}}{4}\\right)$'\n\n.. math ::\n\n    \\left(\\frac{5 - \\frac{1}{x}}{4}\\right)\n\nRadicals\n--------\n\nRadicals can be produced with the ``\\sqrt[]{}`` command.  For example::\n\n    r'$\\sqrt{2}$'\n\n.. math ::\n\n    \\sqrt{2}\n\nAny base can (optionally) be provided inside square brackets.  Note that the\nbase must be a simple expression, and can not contain layout commands such as\nfractions or sub/superscripts::\n\n    r'$\\sqrt[3]{x}$'\n\n.. math ::\n\n    \\sqrt[3]{x}\n\n\nFonts\n-----\n\nThe default font is *italics* for mathematical symbols.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>This default can be changed using the ``mathtext.default`` rcParam.  This is\n   useful, for example, to use the same font as regular non-math text for math\n   text, by setting it to ``regular``.</p></div>\n\nTo change fonts, e.g., to write \"sin\" in a Roman font, enclose the text in a\nfont command::\n\n    r'$s(t) = \\mathcal{A}\\mathrm{sin}(2 \\omega t)$'\n\n\\begin{align}s(t) = \\mathcal{A}\\mathrm{sin}(2 \\omega t)\\end{align}\n\nMore conveniently, many commonly used function names that are typeset in\na Roman font have shortcuts.  So the expression above could be written as\nfollows::\n\n    r'$s(t) = \\mathcal{A}\\sin(2 \\omega t)$'\n\n\\begin{align}s(t) = \\mathcal{A}\\sin(2 \\omega t)\\end{align}\n\nHere \"s\" and \"t\" are variable in italics font (default), \"sin\" is in Roman\nfont, and the amplitude \"A\" is in calligraphy font.  Note in the example above\nthe calligraphy ``A`` is squished into the ``sin``.  You can use a spacing\ncommand to add a little whitespace between them::\n\n    r's(t) = \\mathcal{A}\\/\\sin(2 \\omega t)'\n\n.. Here we cheat a bit: for HTML math rendering, Sphinx relies on MathJax which\n   doesn't actually support the italic correction (\\/); instead, use a thin\n   space (\\,) which is supported.\n\n\\begin{align}s(t) = \\mathcal{A}\\,\\sin(2 \\omega t)\\end{align}\n\nThe choices available with all fonts are:\n\n    ========================= ================================\n    Command                   Result\n    ========================= ================================\n    ``\\mathrm{Roman}``        :mathmpl:`\\mathrm{Roman}`\n    ``\\mathit{Italic}``       :mathmpl:`\\mathit{Italic}`\n    ``\\mathtt{Typewriter}``   :mathmpl:`\\mathtt{Typewriter}`\n    ``\\mathcal{CALLIGRAPHY}`` :mathmpl:`\\mathcal{CALLIGRAPHY}`\n    ========================= ================================\n\n.. role:: math-stix(mathmpl)\n   :fontset: stix\n\nWhen using the `STIX <http://www.stixfonts.org/>`_ fonts, you also have the\nchoice of:\n\n    ================================ =========================================\n    Command                          Result\n    ================================ =========================================\n    ``\\mathbb{blackboard}``          :math-stix:`\\mathbb{blackboard}`\n    ``\\mathrm{\\mathbb{blackboard}}`` :math-stix:`\\mathrm{\\mathbb{blackboard}}`\n    ``\\mathfrak{Fraktur}``           :math-stix:`\\mathfrak{Fraktur}`\n    ``\\mathsf{sansserif}``           :math-stix:`\\mathsf{sansserif}`\n    ``\\mathrm{\\mathsf{sansserif}}``  :math-stix:`\\mathrm{\\mathsf{sansserif}}`\n    ================================ =========================================\n\nThere are also three global \"font sets\" to choose from, which are\nselected using the ``mathtext.fontset`` parameter in `matplotlibrc\n<matplotlibrc-sample>`.\n\n``cm``: **Computer Modern (TeX)**\n\n![](../../_static/cm_fontset.png)\n\n\n``stix``: **STIX** (designed to blend well with Times)\n\n![](../../_static/stix_fontset.png)\n\n\n``stixsans``: **STIX sans-serif**\n\n![](../../_static/stixsans_fontset.png)\n\n\nAdditionally, you can use ``\\mathdefault{...}`` or its alias\n``\\mathregular{...}`` to use the font used for regular text outside of\nmathtext.  There are a number of limitations to this approach, most notably\nthat far fewer symbols will be available, but it can be useful to make math\nexpressions blend well with other text in the plot.\n\nCustom fonts\n~~~~~~~~~~~~\n\nmathtext also provides a way to use custom fonts for math.  This method is\nfairly tricky to use, and should be considered an experimental feature for\npatient users only.  By setting the rcParam ``mathtext.fontset`` to ``custom``,\nyou can then set the following parameters, which control which font file to use\nfor a particular set of math characters.\n\n    ============================== =================================\n    Parameter                      Corresponds to\n    ============================== =================================\n    ``mathtext.it``                ``\\mathit{}`` or default italic\n    ``mathtext.rm``                ``\\mathrm{}`` Roman (upright)\n    ``mathtext.tt``                ``\\mathtt{}`` Typewriter (monospace)\n    ``mathtext.bf``                ``\\mathbf{}`` bold italic\n    ``mathtext.cal``               ``\\mathcal{}`` calligraphic\n    ``mathtext.sf``                ``\\mathsf{}`` sans-serif\n    ============================== =================================\n\nEach parameter should be set to a fontconfig font descriptor (as defined in the\nyet-to-be-written font chapter).\n\n.. TODO: Link to font chapter\n\nThe fonts used should have a Unicode mapping in order to find any\nnon-Latin characters, such as Greek.  If you want to use a math symbol\nthat is not contained in your custom fonts, you can set the rcParam\n``mathtext.fallback_to_cm`` to ``True`` which will cause the mathtext system\nto use characters from the default Computer Modern fonts whenever a particular\ncharacter can not be found in the custom font.\n\nNote that the math glyphs specified in Unicode have evolved over time, and many\nfonts may not have glyphs in the correct place for mathtext.\n\nAccents\n-------\n\nAn accent command may precede any symbol to add an accent above it.  There are\nlong and short forms for some of them.\n\n    ============================== =================================\n    Command                        Result\n    ============================== =================================\n    ``\\acute a`` or ``\\'a``        :mathmpl:`\\acute a`\n    ``\\bar a``                     :mathmpl:`\\bar a`\n    ``\\breve a``                   :mathmpl:`\\breve a`\n    ``\\ddot a`` or ``\\''a``        :mathmpl:`\\ddot a`\n    ``\\dot a`` or ``\\.a``          :mathmpl:`\\dot a`\n    ``\\grave a`` or ``\\`a``        :mathmpl:`\\grave a`\n    ``\\hat a`` or ``\\^a``          :mathmpl:`\\hat a`\n    ``\\tilde a`` or ``\\~a``        :mathmpl:`\\tilde a`\n    ``\\vec a``                     :mathmpl:`\\vec a`\n    ``\\overline{abc}``             :mathmpl:`\\overline{abc}`\n    ============================== =================================\n\nIn addition, there are two special accents that automatically adjust to the\nwidth of the symbols below:\n\n    ============================== =================================\n    Command                        Result\n    ============================== =================================\n    ``\\widehat{xyz}``              :mathmpl:`\\widehat{xyz}`\n    ``\\widetilde{xyz}``            :mathmpl:`\\widetilde{xyz}`\n    ============================== =================================\n\nCare should be taken when putting accents on lower-case i's and j's.  Note that\nin the following ``\\imath`` is used to avoid the extra dot over the i::\n\n    r\"$\\hat i\\ \\ \\hat \\imath$\"\n\n\\begin{align}\\hat i\\ \\ \\hat \\imath\\end{align}\n\nSymbols\n-------\n\nYou can also use a large number of the TeX symbols, as in ``\\infty``,\n``\\leftarrow``, ``\\sum``, ``\\int``.\n\n.. math_symbol_table::\n\nIf a particular symbol does not have a name (as is true of many of the more\nobscure symbols in the STIX fonts), Unicode characters can also be used::\n\n   ur'$\\u23ce$'\n\nExample\n-------\n\nHere is an example illustrating many of these features in context.\n\n.. figure:: ../../gallery/pyplots/images/sphx_glr_pyplot_mathtext_001.png\n   :target: ../../gallery/pyplots/pyplot_mathtext.html\n   :align: center\n   :scale: 50\n\n   Pyplot Mathtext\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
